function createToken () {
    let startTime = new Date().getTime()
    let endTime = new Date();
    endTime.setTime(endTime.getTime()+24*60*60*1000);
    let str = startTime + '/' + endTime.getTime()
    const buff = Buffer.from(str, 'utf-8');

// encode buffer as Base64
    const base64 = buff.toString('base64');
    return base64
}

function verificationToken (token, pathname) {
    let author = false

    if (token) {
        const buff = Buffer.from(token, 'base64');
         author = true

        let str = buff.toString('utf-8');
        let nowDay = new Date().getTime()
        // nowDay.setTime(nowDay.getTime()+1*1000);
        let tokens = str.split('/')
        if (nowDay >= Number(tokens[1])) {
            author = false
        }
    } else {
        author = authorizePath(pathname)
    }

    return author

}

function authorizePath (path) {
    // let url = path.slice('?')[0]
    let urlList = ['/api/logonIn', '/api/userLogin', '/api/loginCode', '/api/bookRecommend', '/api/uploadImg']
    let authorize = false
    for(let i in urlList) {
        if (urlList[i] == path) {
            authorize = true
        }
    }
    return authorize
}

module.exports = {
    createToken,
    verificationToken,
    authorizePath
}
